package com.bruce.phoneFlash;

import android.app.Service;
import android.content.Intent;
import android.hardware.camera2.CameraAccessException;
import android.os.IBinder;

import com.bruce.timerTask.MyTimerTask;

import java.util.Timer;
import java.util.TimerTask;

public class MyServiceSMS extends Service {
    private Timer timer;
    private TimerTask openTask;
    private TimerTask closeTask;
    private MyTimerTask task;
    public MyServiceSMS() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        MyApplication app= (MyApplication) getApplication();
        if (app.getIsSMS()&&app.getIsLow()) {
            task=MyTimerTask.getTask();
            timer=new Timer();
            openTask=task.getOpenTask();
            closeTask=task.getCloseTask();
            flash();
        }
    }

    private void flash() {
        timer.schedule(openTask,0);
        timer.schedule(closeTask,500);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
              stopSelf();
            }
        },1000);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (timer != null) {
            timer.cancel();
        }
        if (openTask != null) {
            openTask.cancel();
        }
        if (closeTask != null) {
            closeTask.cancel();
        }
        if (task != null) {
            try {
                task.free();
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

}
